package client;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import message.Message;
import message.MessageParser;
import message.types.stalker.LoginSuccessAnswer;
import message.types.string.LoginRequest;
import message.types.stringstring.LoginFailedAnswer;
import network.e2e.FrameReader;
import network.e2e.FrameWriter;
import network.link.FrameInputStream;
import network.link.FrameOutputStream;
import stalker.Stalker;
import client.gui.workbox.WorkBox;
import client.network.ServiceRequestCenter;


public class Main {
  
  private static Socket sc = null;
  
  public static void main(String[] args) {
    Stalker.SERVER = false;
    
    String hostName = "192.168.0.101";
    int port = 4444;
    
    if (sc != null) {
      try {
        sc.close();
      } catch (IOException e) {
        System.out.println("Tolerated error");
        e.printStackTrace();
      }
    }
    
    sc = null;
    try {
      sc = new Socket(hostName, port);
    } catch (UnknownHostException e) {
      System.out.println("Error : Host not detected");
      e.printStackTrace();
      System.exit(1);
    } catch (IOException e) {
      System.out.println("Error : Socket opening failed due to I/O Error");
      e.printStackTrace();
      System.exit(1);
    }
    FrameReader reader = null;
    FrameWriter writer = null;
    try {
      reader = new FrameReader(new FrameInputStream(sc.getInputStream()));
    } catch (IOException e) {
      System.out.println("Error : Failed opening Input Stream");
      e.printStackTrace();
      System.exit(1);
    }
    try {
      writer = new FrameWriter(new FrameOutputStream(sc.getOutputStream()));
    } catch (IOException e) {
      System.out.println("Error : Failed opening Output Stream");
      e.printStackTrace();
      System.exit(1);
    }
    String username = "";
    Stalker credential = null;
    while (true) {
      username = JOptionPane.showInputDialog("Login");
      if (!(username.equals(""))) {
        try {
          MessageParser.send(new LoginRequest(Stalker.GUEST_STALKER, Stalker.LOGIN_MANAGER_STALKER, username), writer);
        } catch (IOException e) {
          System.out.println("Error : Message send error");
          e.printStackTrace();
          System.exit(1);
        }
        Message reply = null;
        try {
          reply = MessageParser.retrieve(reader);
        } catch (IOException e) {
          System.out.println("Error : Message send error");
          e.printStackTrace();
          System.exit(1);
        }
        if (reply instanceof LoginSuccessAnswer) {
          LoginSuccessAnswer answer = (LoginSuccessAnswer)reply;
          System.out.println(answer);
          assert answer.getCredential().getName().equals(username);
          // login successful
          credential = answer.getCredential();
          break;
        } else if (reply instanceof LoginFailedAnswer) {
          LoginFailedAnswer answer = (LoginFailedAnswer) reply;
          assert answer.getName().equals(username);
          JOptionPane.showMessageDialog(new JFrame(),
              "Login failed because " + answer.getReason(),
              "Request rejected",
              JOptionPane.WARNING_MESSAGE);
        }
      }
    }
    System.out.println(credential);
    ServiceRequestCenter servicer = new ServiceRequestCenter(credential, reader, writer);
    WorkBox workbox = new WorkBox(credential.getName(), servicer);
    servicer.setWorkBox(workbox);
    workbox.setVisible(true);
    
    
  }
  
}
